<template name="WxShare">
	<view>
		<view :class="{'fix-bottom':true}" ref="footerLogo" :style="style">
			<!-- <footer-logo></footer-logo> -->
		</view>
	</view>
</template>

<script>
	import FooterLogo from './footer-logo.vue'
	var jweixin = require('jweixin-module')
	export default {
		props: {
			title: String,
			desc: String,
			link: String,
			imgUrl: String
		},
		components:{
			FooterLogo
		},
		data() {
			return {
				pageFromUid: 0,
				style:{}
			}
		},
		mounted() {
			this.setWxShare()
			this.getElementTop()
		},
		methods: {
			getElementTop(){
				// setTimeout(()=>{
				// 	uni.createSelectorQuery().select('.fix-bottom').boundingClientRect((rect) => {
				// 	    const distanceToTop = rect.top;
				// 		const height = rect.height;
				// 	    console.log(distanceToTop,height);
				// 		const offset = distanceToTop+height+50
				// 		this.style = {'paddingTop': 'calc(100vh - '+offset+'px)'}
				// 		// this.style = {height:'calc(100vh - '+distanceToTop+')'}
				// 	  }).exec();
				// },1000)
			},
			buildShareUrl(fromUid) {
				let link = '';
				if (!this.link || this.link === undefined || this.link === 'undefined' || this.link === '/') {
					// link = location.href.split('#')[0];
					link = location.href;
				} else {
					link = this.link;
				}
				const name = 'from_uid';
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
				var qs = link.split('?');
				if (qs.length > 1) {
					var r = qs[1].match(reg);
					if (r != null) {
						this.pageFromUid = parseInt(r[2]);
						var re = eval('/(' + name + '=)([^&]*)/gi');
						link = link.replace(re, name + '=' + fromUid);
					} else if (link.indexOf("?") != -1) {
						link = link + '&from_uid=' + fromUid
					} else {
						link = link + '?from_uid=' + fromUid;
					}
				} else {
					if (link.indexOf("?") != -1) {
						link = link + '&from_uid=' + fromUid
					} else {
						link = link + '?from_uid=' + fromUid;
					}
				}
				return link;
			},
			setWxShare() {
				let that = this;

				this.$u.post('/shopping/wx-js-sdk-config', {
					current_page: location.href.split('#')[0]
				}).then(res => {
					if(!res.current_uid){
						return;
					}
					console.log(res)
					let link = that.buildShareUrl(res.current_uid); // 将当前用户的uid写入分享出去的链接

					jweixin.config(res);
					jweixin.ready(function() {
						// TODO
						console.log('wx-is ready')
						jweixin.updateAppMessageShareData({
							title: that.title, // 分享标题
							desc: that.desc, // 分享描述
							link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
							imgUrl: that.imgUrl, // 分享图标
							success: function() {
								// 设置成功
							},
							fail: function(e) {}
						})
						jweixin.updateTimelineShareData({
							title: that.title, // 分享标题
							link: link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
							imgUrl: that.imgUrl, // 分享图标
							success: function() {
								// 设置成功
							},
							fail: function(e) {}
						})
					});
					jweixin.error(function(res) {
						console.log('wx-is error')
						console.log(res)
						// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
					});
					// 如果当前页面带有from_uid并且from_uid不是当前用户，提交一次绑定邀请人，如果当前用户已经有邀请人，则服务端不处理就可以了
					if (this.pageFromUid && this.pageFromUid != res.current_uid) {
						this.$u.post('/user/bind-referer',{referral_id:this.pageFromUid})
					}
				}).catch(e => {

				})
			}
		}
	}
</script>

<style>
.fix-bottom {
	/* position: sticky;
	top:cacl(100vh - 170px);
	left:0;
	right:0;
	padding-bottom: 50px; */
}
</style>
